################################################################################
# Judicial Ideology as Text -- Figure A5: Dynamic Ideal Points v. Martin Quinn
# Truscott & Romano
# Updated February 2025
################################################################################


################################################################################
# Load Source
################################################################################

source('R/truscott_romano_source.R') # Load Source


################################################################################
# Figure A5: Dynamic Ideal Points v. Martin Quinn (Z-Score Normalization of Absolute Values)
################################################################################


{

  combined_dynamic <- data.frame()

  for (weight in 1:length(wordshoals_dynamic)){

    temp_dynamic <- wordshoals_dynamic[[weight]]

    for (term in 1:length(temp_dynamic)){

      combined_dynamic <- bind_rows(combined_dynamic, temp_dynamic[[term]]$summary_output %>% mutate(term = names(temp_dynamic[term]), weight = names(wordshoals_dynamic[weight])))

    }


  }

  combined_dynamic <- combined_dynamic %>% filter(weight == 'High')

  mq_summary <- mq %>%
    dplyr::select(term, justiceName, post_mn, post_025, post_975) %>%
    filter(term >= 2005) %>%
    filter(term <= 2021) %>%
    mutate(author= sub(".*([A-Z])", "\\1", justiceName),
           author = ifelse(author == "Connor", "O'Connor", author)) %>%
    dplyr::select(author, term, post_mn, post_025, post_975) %>%
    group_by(author) %>%
    summarise(mean_mq = mean(post_mn),
              mean_lower = mean(post_025),
              mean_upper = mean(post_975))

  correlation <- combined_dynamic %>%
    filter(!author == 'Rehnquist') %>%
    group_by(author) %>%
    mutate(
      lower_ci = theta - 1.96 * se,
      upper_ci = theta + 1.96 * se,
      term = as.numeric(term),
      author = ifelse(author == 'Connor', "O'Connor", author)) %>%
    summarise(
      mean_theta = mean(theta, na.rm = TRUE),
      lower_ci_theta = mean(theta, na.rm = TRUE) - 1.96 * mean(se, na.rm = TRUE),
      upper_ci_theta = mean(theta, na.rm = TRUE) + 1.96 * mean(se, na.rm = TRUE)
    ) %>%
    mutate(mean_theta = as.numeric(mean_theta),
           ideology = ifelse(author %in% c('Ginsburg', 'Sotomayor', 'Breyer', 'Kagan'), 'Democratic Appointee', 'Republican Appointee')) %>%
    left_join(mq_summary, by = 'author') %>%
    mutate(
      mean_theta_scaled = (mean_theta - mean(mean_theta)) / sd(mean_theta),
      mean_mq_scaled = (mean_mq - mean(mean_mq)) / sd(mean_mq)) %>%
    dplyr::select(author, mean_theta_scaled, mean_mq_scaled)

  correlation <- cor(correlation$mean_theta_scaled, correlation$mean_mq_scaled, method = 'pearson')

  figure_A5 <- combined_dynamic %>%
    filter(!author == 'Rehnquist') %>%
    group_by(author) %>%
    mutate(
      lower_ci = theta - 1.96 * se,
      upper_ci = theta + 1.96 * se,
      term = as.numeric(term),
      author = ifelse(author == 'Connor', "O'Connor", author)) %>%
    summarise(
      mean_theta = mean(theta, na.rm = TRUE),
      lower_ci_theta = mean(theta, na.rm = TRUE) - 1.96 * mean(se, na.rm = TRUE),
      upper_ci_theta = mean(theta, na.rm = TRUE) + 1.96 * mean(se, na.rm = TRUE)
    ) %>%
    mutate(mean_theta = as.numeric(mean_theta),
           ideology = ifelse(author %in% c('Ginsburg', 'Sotomayor', 'Breyer', 'Kagan'), 'Democratic Appointee', 'Republican Appointee')) %>%
    left_join(mq_summary, by = 'author') %>%
    mutate(
      mean_theta_scaled = (mean_theta - mean(mean_theta)) / sd(mean_theta),
      mean_mq_scaled = (mean_mq - mean(mean_mq)) / sd(mean_mq)) %>% # Z-score Standardization
    dplyr::select(author, mean_theta_scaled, mean_mq_scaled, ideology) %>%
    mutate(mean_theta_scaled = abs(mean_theta_scaled),
           mean_mq_scaled = abs(mean_mq_scaled)) %>%
    ggplot(aes(x = mean_theta_scaled, y = mean_mq_scaled)) +
    geom_segment(x = 0, xend = 1.75, y = 0, yend = 1.75, size = 1.25) +
    geom_point(aes(fill = ideology), colour = 'black', shape = 21, size = 3) +
    scale_fill_manual(values = c('deepskyblue3', 'red3')) +
    labs(x = '\nWordshoal (Scaled)',
         y = 'Martin-Quinn (Scaled)\n') +
    scale_y_continuous(lim = c(0, 1.75), breaks = seq(0, 1.5, 0.5)) +
    scale_x_continuous(lim = c(0, 1.75), breaks = seq(0, 1.5, 0.5)) +
    ggrepel::geom_label_repel(aes(label = author), label.padding = 0.25) +
    theme_minimal() +
    theme(panel.border = element_rect(size = 1, colour = 'black', fill = NA),
          axis.text = element_text(size = 12, colour = 'black'),
          axis.title = element_text(size = 14, colour = 'black'),
          legend.position = 'bottom',
          legend.text = element_text(size = 14, colour = 'black'),
          legend.box.background = element_rect(size = 1, colour = 'black', fill = NA),
          legend.title = element_blank()) +
    ggplot2::annotate('label', x = 0.15, y = 1.5, label = 'More Ideological\n(Martin-Quinn)', size = 5) +
    ggplot2::annotate('label', x = 1.5, y = 0.1, label = 'More Ideological\n(Dynamic Wordshoal)', size = 5)

  rm(mq_summary, combined_dynamic, correlation, mq, term, weight)


  ggsave(figure_A5, file = 'figures/Figure_A5.png',
         width = 10,
         height = 8,
         units = 'in',
         bg = 'white')


  ggsave('figures_tiff/Figure_A5.tiff',
         plot = figure_A5,
         device = 'tiff',
         dpi = 600,
         width = 10,
         height = 10,
         units = 'in',
         bg = 'white')

} # Figure A5: Dynamic Ideal Points v. Martin-Quinn (Z-Score Normalized, Absolute Values)
